package org.monkey.example.websocket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import org.monkey.common.cbs.util.PageConvertUtil;
import org.monkey.common.constant.entity.ErrorResultEnum;
import org.monkey.common.constant.exception.BaseException;
import org.monkey.common.constant.util.DataUtil;
import org.monkey.example.websocket.dao.SocketMsgDao;
import org.monkey.example.websocket.entity.convert.SocketMsgConvert;
import org.monkey.example.websocket.entity.dto.SocketMsgDTO;
import org.monkey.example.websocket.entity.form.SocketMsgAddForm;
import org.monkey.example.websocket.entity.form.SocketMsgListForm;
import org.monkey.example.websocket.entity.form.SocketMsgPageForm;
import org.monkey.example.websocket.entity.form.SocketMsgSaveForm;
import org.monkey.example.websocket.entity.form.SocketMsgUpdateForm;
import org.monkey.example.websocket.entity.po.SocketMsgPO;
import org.monkey.example.websocket.service.SocketMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SocketMsgServiceImpl implements SocketMsgService {

  @Autowired
  SocketMsgDao socketMsgDao;
  @Autowired
  SocketMsgConvert socketMsgConvert;

  @Override
  public SocketMsgDTO getById(Long id, boolean nullThrow) {
    SocketMsgPO socketMsgPo = socketMsgDao.selectById(id);
    if (socketMsgPo == null && nullThrow) {
      throw new BaseException(ErrorResultEnum.DATA_NOT_FOUNT);
    }
    return socketMsgConvert.po2Dto(socketMsgPo);
  }

  @Override
  public SocketMsgDTO add(SocketMsgAddForm addForm) {
    SocketMsgDTO socketMsgDto = socketMsgConvert.addForm2Dto(addForm);
    SocketMsgPO socketMsgPo = socketMsgConvert.dto2Po(socketMsgDto);
    socketMsgDao.insert(socketMsgPo);
    return socketMsgConvert.po2Dto(socketMsgPo);
  }

  @Override
  public SocketMsgDTO update(SocketMsgUpdateForm updateForm) {
    SocketMsgDTO socketMsgDto = socketMsgConvert.updateForm2Dto(updateForm);
    SocketMsgPO socketMsgPo = socketMsgConvert.dto2Po(socketMsgDto);
    socketMsgDao.updateById(socketMsgPo);
    return socketMsgConvert.po2Dto(socketMsgPo);
  }

  @Override
  public SocketMsgDTO save(SocketMsgSaveForm saveForm) {
    SocketMsgDTO socketMsgDto = socketMsgConvert.saveForm2Dto(saveForm);
    SocketMsgPO socketMsgPo = socketMsgConvert.dto2Po(socketMsgDto);
    if (socketMsgDto.getId() == null) {
      socketMsgDao.insert(socketMsgPo);
    } else {
      socketMsgDao.updateById(socketMsgPo);
    }
    return socketMsgConvert.po2Dto(socketMsgPo);
  }

  @Override
  public List<SocketMsgDTO> listAll() {
    List<SocketMsgPO> pos = socketMsgDao.selectList(new QueryWrapper<>());
    return DataUtil.colTypeChange(pos, po -> socketMsgConvert.po2Dto(po));
  }

  @Override
  public IPage<SocketMsgDTO> page(SocketMsgPageForm pageForm) {
    Page<SocketMsgPO> page = new Page<>(pageForm.getPageNumber(),
        pageForm.getPageSize());
    IPage<SocketMsgPO> iPage = socketMsgDao
        .page(page, pageForm);
    return PageConvertUtil.convert(iPage, po -> socketMsgConvert.po2Dto(po));
  }

  @Override
  public List<SocketMsgDTO> list(SocketMsgListForm listForm) {
    List<SocketMsgPO> pos = socketMsgDao.list(listForm);
    return DataUtil.colTypeChange(pos, po -> socketMsgConvert.po2Dto(po));
  }

  @Override
  public Boolean delete(Long id) {
    socketMsgDao.deleteById(id);
    return true;
  }
}
